<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <!--{include file='comm/header.tpl'}-->
    <!--{include file='comm/comm_editor.tpl'}-->
    <title>商品配件</title>
</head>

<body>

<div class="container">
 
    <div class="comm_content">

            <div class="search-box">
                <div style="position:relative">
                    <form id="search_form" onsubmit="return false;" class="form-inline">
                    	<span>
                        	配件名称：<input type="text" name="fitName" id="fitName1" class="form-control w150" />
                        </span>

                        <span>
                        	状态：
                            <select  class="form-control w150" id="useFlg_search" name="useFlg">
                                <option value="">请选择</option>
                                <option value="1">启用</option>
                                <option value="0">禁用</option>
                            </select>
                        </span>

                        <span>
                        	<input type="button" value="查询" class="btn btn-primary" onclick="goodsFittings.search();" />
                        </span>
                        <span>
                            <button onclick="goodsFittings.clearUp();" id="clearBtn" class="btn btn-warning" type="button">重置</button>
                        </span>
                    </form>
                </div>
            </div>

            <table id="flex" style="display:none"></table>

        </div>

</div>

<div id="pop" style="display: none;">

    <form class="form-horizontal" id="editForm" onsubmit="return false;">

        <div class="form-group">
            <label for="fitName" class="col-sm-3 control-label"><span class="red">*</span> 配件名称：</label>
            <div class="col-sm-9">
                <input type="text" class="form-control w300" id="fitName" name="fitName" />
            </div>
        </div>

        <div class="form-group">
            <label for="fitUnit" class="col-sm-3 control-label"><span class="red">*</span> 配件单位：</label>
            <div class="col-sm-9">
                <select name="fitUnit" id="fitUnit" class="form-control w150">
                    <option value="1">套</option>
                    <option value="2">份</option>
                    <option value="3">个</option>
                    <option value="4">包</option>
                    <option value="5">支</option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label for="sortOrder" class="col-sm-3 control-label"><span class="red">*</span> 排序：</label>
            <div class="col-sm-9">
                <input type="text" class="form-control w150" id="sortOrder" name="sortOrder" />
            </div>
        </div>

        <div class="form-group">
            <label for="image" class="col-sm-3 control-label"> 配件图片：</label>
            <div class="col-sm-9">
                <div style="width:100%;">
                    <div style="width:80px;height:80px;">
                        <img src=""  id="image" style="width:80px;height:80px;" >
                    </div>
                    <div style="padding-top:5px;text-align: left;">
                        <input type="button" class="btn btn-primary" value="上传" id="uploadPic">
                        <input type="button" class="btn btn-warning" value="删除" onclick="goodsFittings.deletePic();">&nbsp;
                        <input type="hidden" name="hidImg" id="hidImg" value="" >
                    </div>
                </div>
            </div>
        </div>

        <input type="hidden" name="useFlg" id="useFlg" value="1" />
        <input type="hidden" name="fitID" id="fitID" value="" />

    </form>

</div>

<script type="text/javascript">
$("#flex").flexigrid({
    url:"/invokingAjax-backend/goods/goodsFittings.ajax?operFlg=1&xjx=none",
    dataType: "json",
    colModel : [
        <!--{if $edit_permission}-->
        {display: "操作", width : 100, align: "center"},
        <!--{/if}-->
        {display: "配件名称", width : 180},
        {display: "配件单位", width : 180 },
        {display: "排序",width : 100, name : "sortOrder", sortable : true},
        {display: "使用状态", width : 100 , align: "center"}
    ],
    <!--{if $edit_permission}-->
    buttons : [
        {name: "添加", bclass: "add", onpress : "goodsFittings.edit"},
        {separator: true},
        {name: "删除", bclass: "delete", onpress : "goodsFittings.deleteFittings"}
    ],
    <!--{/if}-->
    sortname: "sortOrder",
    sortorder: "asc",
    usepager: true,
    rp: 10,
    useRp: true,
    showTableToggleBtn: true,
    onSubmit: function() {
        var dt = $('#search_form').serializeArray();
        $("#flex").flexOptions({params: dt});
        return true;
    }
});

var goodsFittings = {

    delete_str: "" ,
    useFlg_str : "" ,
    dt:"",
    //搜索配件
    search: function() {
        var dt = $('#search_form').serializeArray();
        $("#flex").flexOptions({newp: 1,params: dt}).flexReload();
    },

    deleteFittings: function() {
        this.delete_str = "";
        $.each($("#flex").find("tr"), function(k, v) {
            if ($(v).hasClass("trSelected")) {
                goodsFittings.delete_str += "_" + $(v).attr("id").replace("row", "");
            }
        });

        if (this.delete_str == "")
            E.alert("请选择需要删除的商品配件");
        else
            E.confirm("删除当前商品配件，您确认删除吗？", "goodsFittings.deleteConfirm");
    },

    doAction: function( fitID , useFlg ) {
        this.fitID = fitID;
        this.useFlg = useFlg;
        if( useFlg == 0 )
            E.confirm("您确认要禁用此商品配件吗？", "goodsFittings.actionConfirm");
        else
            goodsFittings.actionConfirm() ;
    },

    actionConfirm : function () {

        E.loadding.open('请稍候...');
        $.ajax({
            type: "POST",
            url: "/invokingAjax-backend/goods/goodsFittings.ajax?operFlg=5",
            dataType: "JSON",
            data : {
                fitID: this.fitID,
                useFlg: this.useFlg
            },
            success: function( result_obj ) {
                E.loadding.close();
                if (result_obj.code == 200) {
                    E.alert(result_obj.message, 2, "goodsFittings.load");
                } else {
                    E.alert(result_obj.message);
                }
            }
        });
    },

    deleteConfirm: function() {
        this.delete_str = this.delete_str.substr(1);
        $.ajax({
            type: "POST",
            url: "/invokingAjax-backend/goods/goodsFittings.ajax?operFlg=4&delete_str=" + goodsFittings.delete_str,
            dataType: "JSON",
            success: function( result_obj ) {
                if (result_obj.code == 200) {
                    E.alert(result_obj.message, 2, "goodsFittings.load");
                } else {
                    E.alert(result_obj.message);
                }
            }
        });
    },

    //编辑或添加弹出框
    edit: function(fitID) {
        E.loadding.open('请稍候...');
        var name="";

        if(fitID){
            name="修改商品配件";
            E.ajax_post({
                url: "/invokingAjax-backend/goods/goodsFittings.ajax?operFlg=6",
                data:{
                    fitID : fitID
                },
                call:"goodsFittings.val"
            });
        }else{
            name="添加商品配件";
            E.ajax_post({
                url:"/invokingAjax-backend/goods/goodsFittings.ajax?operFlg=7" ,
                call:"goodsFittings.sortUp"
            });
        }

        E.popup.open({
            id:"pop",
            title: name,
            css: "width:760px;",
            btnName: "保存",
            btnCall:"goodsFittings.check();"
        });
    },

    //编辑弹出层赋值
    val: function(o){
        E.loadding.close();
        $("#fitName").val(o.fitName);
        $("#fitUnit").val(o.fitUnit);
        $("#sortOrder").val(o.sortOrder);
        $("#fitID").val(o.fitID);
        if(o.image == ""){
            $("#hidImg").val('');
            $("#image").attr('src','/quanliadmin/docroot/images/backend/default.jpg');
        }else{
            $("#hidImg").val(o.image);
            $("#image").attr('src',o.image);
        }
    },

    check: function() {
        this.dt = E.getFormValues("editForm");
        var error_msg = "";

        if (this.dt.fitName == "")
            error_msg += "配件名称不能为空<fvbr />";

        if (this.dt.fitUnit == "")
            error_msg += "配件单位不能为空<br />";

        if (!E.isInt(this.dt.sortOrder))
            error_msg += "排序序号必须为数字<br />";

        if (error_msg != "") {
            E.alert(error_msg);
        }else {
            goodsFittings.addSave() ;
        }
    },

    //保存配件信息
    addSave: function() {
        E.loadding.open("请稍候");
        $.ajax({
            type: "POST",
            url: "/invokingAjax-backend/goods/goodsFittings.ajax?operFlg=2" ,
            dataType: "JSON",
            data: {
                fitID : this.dt.fitID,
                fitName : this.dt.fitName,
                fitUnit : this.dt.fitUnit,
                sortOrder : this.dt.sortOrder,
                hidImg: this.dt.hidImg,
                useFlg : this.dt.useFlg
            },
            success: function( result_obj ) {
                E.loadding.close(); //关闭等待层

                if (result_obj.code == 200) {

                    goodsFittings.load();
                    if(result_obj.fitID){
                        E.alert(result_obj.message, 2, "E.popup.close");
                    }else{
                        $("#fitName").val('');
                        $("#hidImg").val('');
                        $("#image").attr('src','/quanliadmin/docroot/images/backend/default.jpg');
                        $("#sortOrder").val(result_obj.sortOrder);
                        E.alert(result_obj.message, 2);
                    }
                } else {
                    E.alert(result_obj.message);
                }
            }
        });
    },

    //最大序列号重置
    sortUp:function(o){
        E.loadding.close();
        $("#sortOrder").val(o.sortOrder);
    },

    load: function() {
        $("#flex").flexReload();
    },

    clearUp: function(){
        $('#fitName1').val('');
        $('#useFlg_search').val('');
    },

    //删除商品图片
    deletePic:function(){
        $('#image').attr('src', '/quanliadmin/docroot/images/backend/default.jpg');
        $('#hidImg').val('');
    }

};

KindEditor.ready(function(K) {
    var editor = K.editor({
        allowFileManager : true,
        uploadJson: '/framework/kindeditor/php/upload_json.php?act=system/fittings'
    });

    $(document).on('click','#uploadPic',function(){
        editor.loadPlugin('image', function() {
            editor.plugin.imageDialog({
                showRemote : false,
                clickFn : function(url) {
                    $('#image').attr('src', url);
                    $('#hidImg').val(url);
                    editor.hideDialog();
                }
            });
        });
    });
});

</script>

</body>

</html>
